#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

pthread_mutex_t lock1;
pthread_mutex_t lock2;

void* threadA_start(void* arg){
    pthread_detach(pthread_self());
    pthread_mutex_lock(&lock1);
    sleep(2); //等待线程B将2锁拿走

    pthread_mutex_lock(&lock2);

    return NULL;
}

void* threadB_start(void* arg){
    pthread_detach(pthread_self());
    pthread_mutex_lock(&lock2);
    sleep(2); //线程B等待2s， 等待线程A将1锁拿走

    pthread_mutex_lock(&lock1);

    return NULL;
}


int main(){
    pthread_mutex_init(&lock1, NULL);
    pthread_mutex_init(&lock2, NULL);

    pthread_t tid;
    int ret = pthread_create(&tid, NULL, threadA_start, NULL);
    if(ret < 0){
        return 0;
    }
    ret = pthread_create(&tid, NULL, threadB_start, NULL);
    if(ret < 0){
        return 0;
    }

    while(1){
        sleep(1);
    }

    pthread_mutex_destroy(&lock1);
    pthread_mutex_destroy(&lock2);
    return 0;
}
